from django.forms import model_to_dict
from django.views import View
from django.http import JsonResponse
from app.models import *  # 导入你的模型
from datetime import datetime, timedelta, timezone
from django.utils import timezone
import json


class AuthenticationView(View):
    def get(self, request):  # 获取用户认证基本信息
        try:
            id = request.GET.get('id', '暂无数据')  # 获取id
            # 假设我们想要获取第一个用户的信息，或者可以根据其他条件过滤
            if id == '暂无数据':  # 如果id不存在就返回
                response = JsonResponse({"code": "404", "msg": "未传入用户ID"}, status=404)
                response['Cache-Control'] = 'no-cache, no-store, must-revalidate'  # HTTP 1.1
                response['Pragma'] = 'no-cache'  # HTTP 1.0
                response['Expires'] = '0'  # Proxies
                return response
            # 根据ID获取单条数据，并进行返回
            user_profile = User.objects.select_related('userinfo').get(id=id)
            flagList = [  # 获取验证的数据
                user_profile.phone != '',
                user_profile.userinfo.realName != '' and user_profile.userinfo.IDCode != '',
                user_profile.userinfo.diplomaImage != '',
                user_profile.userinfo.carImage != '',
                user_profile.userinfo.houseImage != '',
                user_profile.userinfo.salaryImage != '',
            ]

            response = JsonResponse({"code": "200", "msg": "获取成功", "result": {
                'phone': user_profile.phone,
                'realName': user_profile.userinfo.realName,
                'IDCode': user_profile.userinfo.IDCode,
                'diplomaImage': user_profile.userinfo.diplomaImage,
                'carImage': user_profile.userinfo.carImage,
                'houseImage': user_profile.userinfo.houseImage,
                'salaryImage': user_profile.userinfo.salaryImage,
            }, 'flagList': flagList})
            response['Cache-Control'] = 'no-cache, no-store, must-revalidate'  # HTTP 1.1
            response['Pragma'] = 'no-cache'  # HTTP 1.0
            response['Expires'] = '0'  # Proxies
            return response
        except Exception as e:  # 当try中有报错时返回服务器错误
            response = JsonResponse({"code": "500", "msg": f"服务器错误: {str(e)}"}, status=500)
            response['Cache-Control'] = 'no-cache, no-store, must-revalidate'  # HTTP 1.1
            response['Pragma'] = 'no-cache'  # HTTP 1.0
            response['Expires'] = '0'  # Proxies

            return response

    def post(self, request):  # 提交用户手机认证
        try:
            id = request.POST.get('id', '暂无数据')  # 获取id
            code = request.POST.get('code', '暂无数据')  # 获取前端传入的验证码
            phone = request.POST.get('phone', '暂无数据')  # 获取手机号
            if id == '暂无数据' or code == '暂无数据' or phone == '暂无数据':  # 如果id不存在就返回
                return JsonResponse({"code": "404", "msg": "请填充数据"}, status=404)
            data = User.objects.get(id=id)  # 查找数据
            current_datetime = timezone.now()  # 创建当前时间
            if data.code == '':
                return JsonResponse({'code': '400', 'msg': '请先发送验证码'})
            if (current_datetime > data.countdown):
                return JsonResponse({'code': '400', 'msg': '验证码无效'})
            if code != data.code:
                return JsonResponse({'code': '400', 'msg': '验证不正确'})
            data.phone = phone  # 单条数据的修改
            data.save()  # 保存数据
            return JsonResponse({'code': '200', 'msg': '提交成功'})
        except Exception as e:
            return JsonResponse({"code": "500", "msg": f"服务器错误: {str(e)}"}, status=500)
